﻿@{
    ViewBag.Title = "Users";
}

@section scripts
{  
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
    <style>
    .function-item 
    {
        margin-left:10px;
        margin-right:5px;
    }
    .function-item input
    {
        vertical-align:bottom;
    }
    </style>
}
<form id="form1" runat="server" method="post" style = "width:100%;height:100%">
    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
        <table style="width:100%;">
            <tr>
                <td style="width:100%;">
                    <a class="mini-button" name="expandAll " iconCls="icon-expand" onclick="expandAll" plain="true">展开</a>
                    <a class="mini-button" name="collapseAll " iconCls="icon-collapse" onclick="collapseAll" plain="true">收缩</a>
                    <span class="separator"></span>
                    <a class="mini-button" name="save" iconCls="icon-save" onclick="saveData" plain="true">保存</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                </td>
            </tr>
        </table>       
    </div>   
    <table>
        <tr>
            <td width ="70px" align="right">角色名称: </td>
            <td width ="240px" ><input class="mini-textbox" id ="FAuthorityName" width ="200px" name = "FAuthorityName" readonly="readonly"/></td>
             <td width ="200px" >
                <div id="ck_select" name="ck_select"  class="mini-checkbox" onvaluechanged ="OnSelectAll" text="全选">
             </td>
        </tr>
    </table>
    <div class="mini-fit">
        <div id="treegrid1" class="mini-treegrid" style="width:100%;height:100%;"     
            url=""
            treeColumn="name" idField="id" parentField="pid" resultAsTree="false"  
            allowResize="true" expandOnLoad="true" showTreeIcon="true"
            allowSelect="false" allowCellSelect="false" enableHotTrack="false"
            ondrawcell="ondrawcell">
            <div property="columns">
                <div type="indexcolumn"></div>
                <div name="name" field="name" width="180" >模块名称</div>
                <div field="functions" width="100%">权限</div>
            </div>
        </div>
    </div>
    <input id="FAuthorityID" name="FAuthorityID" class="mini-hidden"/>
    <script>
        mini.parse();
        var tree = mini.get("treegrid1");
        var item_id = getQueryString("id");
        if (item_id != null) {
            mini.getbyName("FAuthorityID").setValue(item_id);
        }
        function FormShown() {
            onLoadData("-1");
        }

        function collapseAll() {
            tree.collapseAll();
        }

        function expandAll() {
            tree.expandAll();
        }        

        function OnSelectAll() {
            if (mini.getbyName("ck_select").getChecked()) {
                onLoadData("1");
            } else {
                onLoadData("0");
            }
        }

        function onLoadData(mSelectType) {
            var FSystemAppID = parent.window.getSystemAppID();            
            var mjson = "[{'FItemID':'" + item_id + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]";
            $.post("GetAuthorityInfo", { json: mjson }, function (data) {
                var mdata = mini.decode(data);
                mini.getbyName("FAuthorityName").setValue(mdata.data[0].FName);
            });
            var mAuthorityID = mini.getbyName("FAuthorityID").getValue();            
            mjson = "[{'mSystemAppID':'" + FSystemAppID + "','FBaseUrlID':'" + "2" + "','FAuthorityID':'"
                + mAuthorityID + "','FSelectType':'" + mSelectType + "'}]";

            //tree.load("GetUserRights?mjson=" + mjson);
            mini.mask({ el: document.body, cls: 'mini-mask-loading', html: '正在加载数据, 请稍等......' });
            $.post('GetUserRights', { mjson: mjson }, function (data) {                
                tree.loadList(mini.decode(data));
                mini.unmask(document.body);
            });

        }

        function saveData() {
            var mAuthorityID = mini.getbyName("FAuthorityID").getValue();
            var data = tree.getData();
            var json = mini.encode(data);
            if (json) {
                mini.mask({ el: document.body, cls: 'mini-mask-loading', html: '保存中, 请稍等......' });
                var param = parent.window.getSystemAppID();
                $.post("SaveUserRights", { mjson: json, mSystemAppID: param, mAuthorityID: mAuthorityID }, function (data) {
                    mini.unmask(document.body);
                    var ret = mini.decode(data);
                    if (ret.status == "1")
                        mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                    else
                        mini.showTips({ content: ret.error, state: "danger", x: "center", y: "top", timeout: 3000 });
                });
            } else {
                mini.alert("请选中一个用户");
            }
        }

        function ondrawcell(e) {
            var tree = e.sender,
                record = e.record,            
                column = e.column,
                field = e.field,
                id = record[tree.getIdField()],
                funs = record.functions;

            function createCheckboxs(funs) {
                if (!funs) return "";
                var html = "";
                for (var i = 0, l = funs.length; i < l; i++) {
                    var fn = funs[i];
                    var clickFn = 'checkFunc(\'' + id + '\',\'' + fn.action + '\', this.checked)';
                    var checked = fn.checked ? 'checked' : '';
                    html += '<label class="function-item"><input onclick="' + clickFn + '" ' + checked
                        + ' type="checkbox" style = "margin-right:5px;" name="'
                        + fn.action + '" hideFocus/>' + fn.name + '</label>';
                }
                return html;
            }

            if (field == 'functions') {
                e.cellHtml = createCheckboxs(funs);
            }
        }

        function getFuns() {
            var data = tree.getData();
            var json = mini.encode(data);
            alert(json);
        }
        function checkFunc(id, action, checked) {
            var record = tree.getRecord(id);
            if(!record) return;
            var funs = record.functions;
            if (!funs) return;
            function getAction(action) {
                for (var i = 0, l = funs.length; i < l; i++) {
                    var o = funs[i];
                    if (o.action == action) return o;
                }
            }
            var obj = getAction(action);
            if (!obj) return;
            obj.checked = checked;
        }
</script>
</form>